package com.zxpan.restaurant.persist.dao.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.SqlSession;

import com.zxpan.restaurant.db.model.Food;
import com.zxpan.restaurant.persist.dao.FoodDao;


public class FoodDaoImpl extends DaoBase<Food> implements FoodDao {

	 private static final String PREFIX = "food";

	
	public FoodDaoImpl() {
		super.setClazz(Food.class);
		super.setPrefix(PREFIX);
	}
	
	@Override
	public Food getFood(String hotelId, String foodId) {
		Map<String, String> map = new HashMap<String, String>();
		map.put("hotelId", hotelId);
		map.put("foodId", foodId);
		Food[] foodArray = super.select(map);
		if(null != foodArray && foodArray.length != 0) {
			return foodArray[0];
		} else {
			return null;
		}
	}

	@Override
	public List<Food> getCategory(String hotelId, String categoryId) {
		Map<String, String> map = new HashMap<String, String>();
		map.put("hotelId", hotelId);
		map.put("categoryId", categoryId);
		Food[] foodArray = super.select(map);
		if(null != foodArray && foodArray.length != 0) {
			List<Food> foodList = new ArrayList<Food>();
			for(Food food : foodArray) {
				foodList.add(food);
			}
			return foodList;
		} else {
			return null;
		}
	}

	@Override
	public List<Food> getHotelFood(String hotelId) {
		Map<String, String> map = new HashMap<String, String>();
		map.put("hotelId", hotelId);
		Food[] foodArray = super.select(map);
		List<Food> foodList = new ArrayList<Food>();
		for(Food food : foodArray) {
			foodList.add(food);
		}
		return foodList;
	}

	@Override
	public List<Food> searchFood(String hotelId, String keyword) {
		SqlSession session = sqlSessionFactory.openSession(true);
		Map<String, String> map = new HashMap<String, String>();
		map.put("hotelId", hotelId);
		map.put("keyword", keyword);
        @SuppressWarnings("unchecked")
        List<Food> resultList = (List<Food>)session.selectList(new StringBuilder(PREFIX).append(".fuzzySearch").toString(), map);
        System.out.println(resultList.size());
        session.close();
        return resultList;
	}

}
